/*
 * Created by SharpDevelop.
 * User: WinUser
 * Date: 10/16/2008
 * Time: 2:27 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Collections;

namespace ChallanSystem
{
	/// <summary>
	/// Description of Order_Products.
	/// </summary>
	public partial class Order_Products : Form
	{
		Product prod = null;
		DataSet ds = null;
		//DataSet ds1 = null;
		DataRow dr = null;
		DataTable dtFiltered = null;
		
		int customer_type = 0;
		
		public int Customer_Type {
			get { return customer_type; }
			set { customer_type = value; }
		}
		
		public object[] formArray = null;
		
		public object[] formDetails
		{
			get { return formArray; }
			set { formArray = value; }
		}
		
		public Order_Products()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Order_ProductsLoad(object sender, EventArgs e)
		{
			
			switch(Customer_Type)
			{
				case 1:
					lblCustType.Text = "Retail";
					break;
				case 2:
					lblCustType.Text = "Semi Retail";
					break;
				case 3:
					lblCustType.Text = "Wholesale";
					break;
				case 4:
					lblCustType.Text = "Semi Wholesale";
					break;
			}
			
			
			prod = new Product();
			ds = prod.View(this.Customer_Type);
			
			//ds1 = prod.GetOrderHistory();
			
			datagridOrderProduct.SetDataBinding(ds,"Products");
		}
		
		void DatagridOrderProductClick(object sender, System.EventArgs e)
		{
			
			try
			{
				if(datagridOrderProduct.CurrentRowIndex > -1)
				{
					dr = ds.Tables[0].DefaultView[datagridOrderProduct.CurrentRowIndex].Row;
					txtPrice.Text = dr[2].ToString();
					
//					cboPrice.Items.Clear();
//					cboPrice.Items.Add(dr[2].ToString());
//					cboPrice.Items.Add(dr["Price_SemiRetail"].ToString());
//					cboPrice.Items.Add(dr["Price_Wholesale"].ToString());
//					cboPrice.Items.Add(dr["Price_SemiWholesale"].ToString());
				}
				//cboPrice.SelectedIndex = 0;
			}
			catch(IndexOutOfRangeException)
			{
				MessageBox.Show("Error Occurred While Processing Your Request.");
			}
		}
		
		void DatagridOrderProductCurrentCellChanged(object sender, System.EventArgs e)
		{
			try
			{
				if(datagridOrderProduct.CurrentRowIndex > -1 && dtFiltered == null)
				{
					dr = ds.Tables[0].DefaultView[datagridOrderProduct.CurrentRowIndex].Row;
					
					txtPrice.Text = dr[2].ToString();
					
//					cboPrice.Items.Clear();
//					cboPrice.Items.Add(dr[2].ToString());
//					cboPrice.Items.Add(dr["Price_SemiRetail"].ToString());
//					cboPrice.Items.Add(dr["Price_Wholesale"].ToString());
//					cboPrice.Items.Add(dr["Price_SemiWholesale"].ToString());
				}
				else
				{
					dr = dtFiltered.DefaultView[datagridOrderProduct.CurrentRowIndex].Row;
					
					txtPrice.Text = dr[2].ToString();
				}
			}
			catch(IndexOutOfRangeException)
			{
				
			}
		}
		
		void BtnAddClick(object sender, EventArgs e)
		{
			if(validate())
			{
				if(isNumericCheck())
				{
					try
					{
						
						object[] dro = new object[5];
						if(txtQuantity.Text != "" && txtTotal.Text != "")
						{
							
							dro[0] = dr["Product_ID"].ToString();
							dro[1] = dr["Product_Name"].ToString();
							dro[2] = txtQuantity.Text;
							dro[3] = txtPrice.Text;
							dro[4] = txtTotal.Text;
						}
						formDetails = dro;
						//Console.WriteLine("The value of i is :-- ");
						this.DialogResult = DialogResult.OK;
						
						this.Close();
					}
					catch(Exception e2)
					{
						MessageBox.Show("Sorry... Cannot Process Your Request "+e2.ToString());
					}
				}
				else
					MessageBox.Show("Please Enter Valid Data !!!");
			}
			else
				MessageBox.Show("All Fields Are Mandatory !!!");
		}
		
		void txtTotalFocus(object sender, EventArgs e)
		{
			//MessageBox.Show(cboPrice.Text);
				
				if(!txtQuantity.Text.Equals("") && !txtPrice.Text.Equals(""))
				{
//				MessageBox.Show("Invalid Input -- total Focus");
					//System.Console.WriteLine("Entering IF");
					//MessageBox.Show("kenil ",cboPrice.Text);
					int i = (int.Parse(txtQuantity.Text)) * (int.Parse(txtPrice.Text));
					//System.Console.WriteLine("The value of i is :-- "+i);
					//MessageBox.Show(Convert.ToString(i));
					txtTotal.Text = Convert.ToString(i);
			}
		}
		
		bool validate()
		{
			bool validate = true;
			
			if(txtQuantity.Text == "" || txtTotal.Text == "" || txtPrice.Text == "" || ds.Tables[0].Rows.Count == 0 )
				validate = false;
			else if(isNumericCheck())
				validate = true;
			else
			{
				//MessageBox.Show("Invalid Input -- Validate");
				validate = false;
			}
			return validate;
		}
		
		bool isNumericCheck()
		{
			bool validate = true;
			
			try
			{
				uint intValue = Convert.ToUInt32(txtTotal.Text);
				uint intValue1 = Convert.ToUInt32(txtQuantity.Text);
			}
			catch(Exception e)
			{
				MessageBox.Show("Invalid Input "+e);
				txtQuantity.Text = "";
				validate = false;
			}
			return validate;
		}
		
		
		void Button1Click(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			Util searchUtil = new Util();
			string filter = string.Empty;
			filter = txtSearch.Text;
			string columnName = string.Empty;
			columnName = "Product_Name";
			//MessageBox.Show("Filter B4 : "+filter);
//			filter = "Customer_Name = '"+ds.Tables[0].DefaultView[0].Row["Customer_Name"].ToString()+"'";
			//MessageBox.Show("Filter After : "+filter);
			dtFiltered = searchUtil.Search(ds.Tables[0], filter, columnName);
			datagridOrderProduct.DataSource = dtFiltered;
		}
	}
}
